//
// AWS Manager - A free AWS console application demonstrating advanced concepts
// Copyright (C) 2008-2010 Sean O'Dell. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.awsmanager.common;

import com.sun.data.provider.RowKey;
import com.sun.data.provider.impl.ObjectListDataProvider;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.jets3t.service.Jets3tProperties;
import org.jets3t.service.S3Service;
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
import org.jets3t.service.model.S3Bucket;
import org.jets3t.service.model.S3Object;
import org.jets3t.service.security.AWSCredentials;

/**
 *
 * @author Sean ODell
 */
public class S3StorageObjectDataProvider extends ObjectListDataProvider {

    private ArrayList s3ObjectList = new ArrayList();
    private S3Service s3Service;

    public S3StorageObjectDataProvider() {
        setList(s3ObjectList);
        setObjectType(S3StorageObject.class);
    }

    public void load(Properties awsProps, Map s3ManageObjectsInfo, S3StorageConnection s3StorageConnection) {

        S3StorageBucketDataProvider s3BucketDataProvider = new S3StorageBucketDataProvider();
        s3BucketDataProvider.load(awsProps, s3StorageConnection);
        ArrayList rowKeysList = (ArrayList) s3ManageObjectsInfo.get("rowKeys");
        clearObjectList();

        if (awsProps == null) {
            return;
        }

        try {
            s3Service = s3StorageConnection.getS3Service();
            for (int i = 0; i < rowKeysList.size(); i++) {

                S3StorageBucket bucket = s3BucketDataProvider.getS3StorageBucket((RowKey) rowKeysList.get(i));
                S3Bucket s3Bucket = (S3Bucket) s3Service.getBucket(bucket.getname());
                S3Object[] objects = s3Service.listObjects(bucket.getname());

                for (int o = 0; o < objects.length; o++) {
                    S3StorageObject s3StorageObject = new S3StorageObject();
                    s3StorageObject.setbucketname(bucket.getname());
                    s3StorageObject.setname(objects[o].getKey());
                    s3StorageObject.setlastModifiedDate(objects[o].getLastModifiedDate());
                    s3StorageObject.setcontentLength(objects[o].getContentLength());
                    getList().add(s3StorageObject);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String deleteS3StorageObjects(List<Map> objectsInfo) {
        String statusMsg = new String();
        for (Map objectInfo : objectsInfo) {
            String bname = (String) objectInfo.get("bname");
            String oname = (String) objectInfo.get("oname");

            try {
                s3Service.deleteObject(bname,oname);
                statusMsg = statusMsg + "Object deleted: " + bname + "/" + oname + "\n";
            } catch (Exception e) {
                e.printStackTrace();
                if (e.getMessage() != null) {
                    statusMsg = statusMsg + "Error: " + e.getMessage() + "\n";
                }
            }
        }
        return statusMsg;
    }

    public S3StorageObject getS3StorageObject(RowKey rowKey) {
        S3StorageObject s3StorageObject = (S3StorageObject) getObject(rowKey);
        return s3StorageObject;
    }
}
